<!-- 护理等级列表页 -->
<template>
  <div class="min-h serveGrade bg-wt">
    <!-- 条件查询表单 -->
    <SearchForm :searchData="params" @handleSearch="handleSearch" @handleReset="handleReset" @handleClear="handleClear">
    </SearchForm>
    <!-- 展示数据的表格 -->
    <TableList :listData="data" @handleBulid="handleBulid" :total="total"
    :pagination="params"
     @handleEdit="handleEdit" @handleClickDelete="handleClickDelete" 
     @handleForbidden="handleForbidden" @getCurrent="getCurrent"></TableList>

    <!-- 新增和编辑对话框 -->
    <DialogForm :title="title" :visible="formVisible" :data="formData"
     @handleClose="handleClose" @handleAdd="handleAdd" :planData="planData"
     @handleEditForm="handleEditForm"
      ref="dialogFrom">
    </DialogForm>

    <!-- 删除按钮弹框 -->
    <OperateDialog :visible="dialogVisible" @handleDelete="handleDelete" @handleClose="handleDeleteClose"></OperateDialog>

    <!-- 启用和禁用弹框 -->
    <Forbidden :visible="ForbiddenDialogVisible" :text="header" @handleClose="handleForbiddenClose" @handleSubmit="handleSubmit"></Forbidden>
  </div>
</template>

<script setup lang="ts">
import SearchForm from './components/SearchForm.vue';
import DialogForm from './components/DialogFrom.vue'
import TableList from './components/TableList.vue';
import { getLevelList, levelAdd, getnursAllPlanList,getLevelDetails ,levelUpdate,levelDelete,levelStatus } from '@/api/serve';
import { MessagePlugin } from 'tdesign-vue-next'
import { ref, onMounted } from 'vue';
import OperateDialog from '@/components/OperateDialog/index.vue'
import Forbidden from '@/components/Forbidden/index.vue';
const data = ref([]);
const planData = ref([]);
const formData=ref({})
const formVisible = ref(false)
const title=ref('');
const dialogFrom=ref({})
const dialogVisible=ref(false)
const ForbiddenDialogVisible=ref(false)
const total=ref(0);

let params = ref({
  pageNum: '1',
  pageSize: '10',
})

//查询全部护理计划
let queryAllPlan = async () => {
  let result = await getnursAllPlanList();
  if (result.code === 200) {
    planData.value = result.data
  }
}

//条件分页查询
let query = async () => {
  let result = await getLevelList(params.value)
  if (result.code === 200) {
    data.value = result.data.records;
    params.value.pageNum = result.data.page;
    params.value.pageSize = result.data.pageSize;
    total.value=result.data.total;
  }
}

//搜索
let handleSearch = () => {
  query();
}

//重置表单
let handleReset = () => {
  delete params.value.name;
  delete params.value.status;
  query();
}

//清空表单输入框
let handleClear = (val) => {
  if (val == 'name') {
    delete params.value.name;
  } else if (val == 'status') {
    delete params.value.status;
  }
  query();
}

//新增弹框
let handleBulid = () => {
  title.value='新增'
  formVisible.value = true;
}
//关闭弹窗
let handleClose = () => {
  formVisible.value = false;
}

//新增护理等级
let handleAdd = async (val) => {
  let result = await levelAdd(val)
  if (result.code === 200) {
    MessagePlugin.success('添加成功')
    query();
    dialogFrom.value.handleClear();
    formVisible.value = false;
  }
}

//打开编辑弹窗
let handleEdit = async(row) => {
  let result=await getLevelDetails(row.id);
  if(result.code===200){
    formData.value=result.data;
    title.value='修改'
    formVisible.value = true;
  }
}

//修改护理等级信息
let handleEditForm=async()=>{
  let result=await levelUpdate(formData.value)
  if(result.code===200){
    MessagePlugin.success('修改成功')
    query();
    dialogFrom.value.handleClear();
    formVisible.value = false;
  }else MessagePlugin.error('修改失败')
}

//打开删除弹框
let handleClickDelete=(row)=>{
  params.value.id=row.id
  dialogVisible.value=true;
}

//删除
let handleDelete=async()=>{
  let result=await levelDelete(params.value.id)
  if(result.code===200){
    MessagePlugin.success('删除成功')
    dialogVisible.value=false;
    query();
  }else MessagePlugin.error('删除失败')
  delete params.value.id;
}

let handleDeleteClose=()=>{
  dialogVisible.value=false;
}
//打开禁用或启用弹窗
const header=ref('');
let handleForbidden=(val)=>{
  params.value.id=val.id;
  params.value.status=val.status;
  if(val.status== 1){
    header.value='确定禁用'
  }else{
    header.value='确定启用'
  }
  ForbiddenDialogVisible.value=true;
}

//关闭启用禁用弹框
let handleForbiddenClose=()=>{
  delete params.value.id;
  delete params.value.status;
  ForbiddenDialogVisible.value=false;
}

//启用禁用
let handleSubmit=async()=>{
  let result=await levelStatus(params.value)
  if(result.code===200){
    MessagePlugin.success('操作成功')
  }else MessagePlugin.error('操作失败')
  handleForbiddenClose();
  query();
}

//分页时
let getCurrent =(pageInfo)=>{
  params.value.pageNum=pageInfo.current;
  params.value.pageSize=pageInfo.pageSize;
  query();
}

onMounted(() => {
  query();
  queryAllPlan();
})
</script>